package com.itheima.consumer.listeners;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Map;

@Slf4j
@Component
public class MqListener {
    // 简单测试MQ（接收）
    //@RabbitListener(queues = "simple.queue")
    public void listenSimpleQueue(String msg){
        System.out.println("消费者收到了simple.queue的消息：【"+msg+"】");
        //throw new RuntimeException("故意的");
    }

    // 使用两个消费者：worker1
    //@RabbitListener(queues = "work.queue")
    public void listenWorkQueue1(String msg) throws InterruptedException {
        System.out.println("消费者1收到了work.queue的消息： 【"+msg+"】");
        Thread.sleep(20);
    }
    // 使用两个消费者：worker2
    //@RabbitListener(queues = "work.queue")
    public void listenWorkQueue2(String msg) throws InterruptedException {
        System.err.println("消费者2收到了work.queue的消息......： 【"+msg+"】");
        Thread.sleep(200);
    }
    // fanout交换机（广播）
    //@RabbitListener(queues = "fanout.queue1")
    public void listenFanoutQueue1(String msg)  {
        System.out.println("消费者1收到了fanout.queue1的消息： 【"+msg+"】");
    }
    // fanout交换机（广播）
    //@RabbitListener(queues = "fanout.queue2")
    public void listenFanoutQueue2(String msg)  {
        System.out.println("消费者2收到了fanout.queue2的消息： 【"+msg+"】");
    }
    // direct交换机（定向路由）
    //@RabbitListener(queues = "direct.queue1")
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue1",durable = "true"),
            exchange = @Exchange(name = "hmall.direct",type = ExchangeTypes.DIRECT),
            key = {"red","blue"}
    ))
    public void listenDirectQueue1(String msg)  {
        System.out.println("消费者1收到了direct.queue1的消息： 【"+msg+"】");
    }
    // direct交换机（定向路由）
    //@RabbitListener(queues = "direct.queue2")
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue2",durable = "true"),
            exchange = @Exchange(name = "hmall.direct",type = ExchangeTypes.DIRECT),
            key = {"red","yellow"}
    ))
    public void listenDirectQueue2(String msg)  {
        System.out.println("消费者2收到了direct.queue2的消息： 【"+msg+"】");
    }
    // topic交换机（定向路由）
    @RabbitListener(queues = "topic.queue1")
    public void listenTopicQueue1(String msg)  {
        System.out.println("消费者1收到了topic.queue1的消息： 【"+msg+"】");
    }
    // topic交换机（定向路由）
    @RabbitListener(queues = "topic.queue2")
    public void listenTopicQueue2(String msg)  {
        System.out.println("消费者2收到了topic.queue2的消息： 【"+msg+"】");
    }

    @RabbitListener(queues = "object.queue")
    public void listenObject(Map<String,Object> msg){
        System.out.println("消费者 收到了object.queue的消息：【"+msg+"】");
    }
    //死信交换机
    @RabbitListener(queues = "dlx.queue")
    public void listenDlxQueue(String msg){
        log.info("dlx.queue的消息：【" + msg +"】");
    }

    //延迟发送
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "delay.queue",durable = "true"),
            exchange = @Exchange(value = "delay.direct",delayed = "true"),
            key = "hi"
    ))
    public void listenDelayQueue(String msg){
        log.info("接收到了延迟队列的消息");
    }
}
